/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.
 * 项目名称 : 网站服务平台
 * 创建日期 : 2017-08-11
 * 修改历史 :
 * 1. [2017-08-11]创建文件 by chenxin
 */
package com.gavinwind.inspay.paycenter.controller;

import com.gavinwind.common.auth.config.ENUM_AUTH_EVENTTYPE;
import com.gavinwind.common.auth.config.ENUM_INSPAY_TENANT;
import com.gavinwind.common.auth.facade.TokenAuthFacadeService;
import com.gavinwind.inspay.base.InspayBaseController;
import com.gavinwind.inspay.common.exception.system.SignErrException;
import com.gavinwind.inspay.common.model.PayRequestDTO;
import com.gavinwind.inspay.common.model.PayResponseDTO;
import com.gavinwind.inspay.paycenter.core.facade.PayRequestDealFacadeService;
import com.gavinwind.inspay.paycenter.core.service.PayCenterCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 【支付请求处理Facade服务实现类】
 * @author chenxin
 * @date 2017-08-11 下午5:17 
 * @version V1.0
 */
@RestController
public class PayRequestDealController extends InspayBaseController implements PayRequestDealFacadeService {
    @Autowired
    private PayCenterCoreService payCenterCoreService;
    @Autowired
    private TokenAuthFacadeService tokenAuthFacadeServcie;
    @RequestMapping(value = "/pay/gatewayPay",method = RequestMethod.GET)
    public String enterThirdGatewayUrl(PayRequestDTO cRequestDTO,String signMsg) {
        //调用支付中心核心服务的获取支付网关URL方法
        try {
            String tSourceCode = cRequestDTO.getSourceCode();
            checkSignMsgValid(tSourceCode, signMsg);
            String tPayGatewayUrl = payCenterCoreService.getPayGatewayUrl(cRequestDTO);
            return tPayGatewayUrl;
        }catch (SignErrException ex){
            logger.error("支付签名验证不合法",ex);
            throw ex;
        }

    }
    @RequestMapping(value = "/pay/payCallback",method = RequestMethod.GET)
    public void payResultCallback() {

    }

    @RequestMapping(value = "/pay/dealPay",method = RequestMethod.POST)
    public PayResponseDTO payInTime(PayRequestDTO cRequestDTO, String signToken) {
        PayResponseDTO tPayResponseDTO = new PayResponseDTO();
        //2.校验支付令牌的合法性
        try {
            String tSourceCode = cRequestDTO.getSourceCode();
            checkSignMsgValid(tSourceCode, signToken);
            tPayResponseDTO = payCenterCoreService.dealPayInTime(cRequestDTO);
        }catch (SignErrException ex){
            logger.error("支付签名验证不合法",ex);
            tPayResponseDTO.setResultCode(""); //TODO
        }catch (Exception ex){
            //TODO
        }finally {
            return tPayResponseDTO;
        }
    }

    //验证签名的合法性
    private void checkSignMsgValid(String sourceCode ,String signMsg) throws SignErrException{
        //获取租户枚举对象
        ENUM_INSPAY_TENANT tTennant  = ENUM_INSPAY_TENANT.getEnumByKey(sourceCode);
        //校验token合法性
        tokenAuthFacadeServcie.checkTokenAccess(tTennant, ENUM_AUTH_EVENTTYPE.PAY,signMsg);
    }
}
